import {createRouter, createWebHashHistory} from "vue-router";

import Home from '@/components/Home.vue'
import About from '@/components/About.vue'
import Login from '@/components/Login.vue'
import welcome from "@/components/Welcome.vue";
import Users from "@/components/user/Users.vue";
import Rights from "@/components/power/Rights.vue";
import Roles from "@/components/power/Roles.vue";
import Cate from "@/components/goods/Cate.vue";
import Params from "@/components/goods/Params.vue";
import GoodsList  from "@/components/goods/List.vue";
import AddGoods from "@/components/goods/AddGoods.vue";
import Order from "@/components/order/order.vue";
import Report from "@/components/report/report.vue";

// 2. 定义一些路由
const routes = [

    { path: '/', redirect: 'login' },
    { path: '/login', component: Login },
    { path: '/home', component: Home ,
        redirect: '/welcome',
        children:[
            { path: '/welcome', component: welcome },
            { path: '/users', component: Users },
            { path: '/rights', component: Rights},
            { path: '/roles', component: Roles },
            { path: '/categories', component: Cate},
            { path: '/params', component: Params},
            { path: '/goods', component: GoodsList},
            { path: '/add_goods', component: AddGoods },
            { path: '/orders', component: Order},
            { path: '/reports', component: Report}
        ]
    },
    { path: '/about', component: About },

]

// 3. 创建路由实例并传递 `routes` 配置
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes: routes, // `routes: routes` 的缩写
})

// 挂载路由导航守卫
router.beforeEach( (to, from, next) => {

    if (to.path === '/login') return next();

    const tokenStr = window.sessionStorage.getItem('token');

    if(!tokenStr) return next('/login');

    next();
})

export default router
